package com.uh.summary.serviceImpl;



import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.uh.calculation.bean.WarnEnterpriseRecord;
import com.uh.calculation.bean.WarnJiSuanBean;
import com.uh.calculation.service.CalculationService;
import com.uh.microservice.core.util.ThreadPool;
import com.uh.summary.entity.WarnBean;
import com.uh.summary.enums.DimensionEnum;
import com.uh.summary.enums.RuleEnum;
import com.uh.summary.enums.RuleTypeEnum;
import com.uh.summary.enums.TableNameEnum;
import com.uh.summary.mapper.WarnEnterpriseRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.EnumUtils;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j
public class ServerThread implements Runnable {

	private WarnEnterpriseRecordMapper warnEnterpriseRecordMapper;

	private CalculationService calculationService;

	Boolean b;

	int limit;int end;RuleTypeEnum ruleType;Integer start;
	WarnBean warnBean;


	private boolean shutdown = false;
    public ServerThread(WarnEnterpriseRecordMapper warnEnterpriseRecordMapper,CalculationService calculationService,WarnBean warnBean,int limit,int end,RuleTypeEnum ruleType,Integer start) {
    	this.warnEnterpriseRecordMapper=warnEnterpriseRecordMapper;
    	this.calculationService=calculationService;
		this.warnBean=warnBean;
		this.limit=limit;
		this.end=end;
		this.ruleType=ruleType;
		this.start=start;
    }
	@Override
	public void run() {
		WarnJiSuanBean warnJiSuanBean = new WarnJiSuanBean();
		List<String> companyCodes = new ArrayList<>();
		//Integer start = warnEnterpriseRecordMapper.getMaxWarnCode();
		//待插入
		List<WarnEnterpriseRecord> list = new ArrayList<>();
		switch (ruleType) {
			case industrial:
				//int limit = 0;
				do {
					list = warnEnterpriseRecordMapper.getIndustrial(limit, 100,null);
					limit += 100;
					/*if(list.size()==0){
						warnBean.setB(false);
						return ;
					}*/
					for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
						List<WarnEnterpriseRecord> qryList = new ArrayList<>();
						warnJiSuanBean = new WarnJiSuanBean();
						warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
						warnJiSuanBean.setWarnEnterpriseRecords(qryList);
						warnJiSuanBean.setSubjectCode(TableNameEnum.B_TYPE.getCode());
						warnJiSuanBean.setWarnEnterpriseRecordMapper(warnEnterpriseRecordMapper);
						calculationService.count(warnJiSuanBean);

					}
					/*if(limit>warnBean.getNum()){
						warnBean.setNum(limit);
					}*/
					if(limit<end==false) {
						break ;
					}
				} while (!list.isEmpty());
				break;
			case investment:
				//limit = 0;
				do {


					list = warnEnterpriseRecordMapper.getInvest(limit, 100, null);

					limit += 100;

					/*if(list.size()==0){
						warnBean.setB(false);
						return ;
					}*/
					for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
						List<WarnEnterpriseRecord> qryList = new ArrayList<>();
						warnJiSuanBean = new WarnJiSuanBean();
						warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
						warnJiSuanBean.setWarnEnterpriseRecords(qryList);
						warnJiSuanBean.setSubjectCode(TableNameEnum._206_TYPE.getCode());
						warnJiSuanBean.setWarnEnterpriseRecordMapper(warnEnterpriseRecordMapper);
						calculationService.count(warnJiSuanBean);
					}
					/*if(limit>warnBean.getNum()){
						warnBean.setNum(limit);
					}*/
					if(limit<end==false){
						break ;
					}
				} while (!list.isEmpty());
				break;
			case trade:
				//limit = 0;
				//贸易-E
				do {
					System.out.println(limit+" "+end);
					list = warnEnterpriseRecordMapper.getTradeE(limit, 100, null);
					if(limit>warnBean.getNum()){
						warnBean.setNum(limit);
					}
					limit += 100;
					for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
						List<WarnEnterpriseRecord> qryList = new ArrayList<>();
						warnJiSuanBean = new WarnJiSuanBean();
						warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
						warnJiSuanBean.setWarnEnterpriseRecords(qryList);
						warnJiSuanBean.setSubjectCode(TableNameEnum.E_TYPE.getCode());
						warnJiSuanBean.setWarnEnterpriseRecordMapper(warnEnterpriseRecordMapper);
						calculationService.count(warnJiSuanBean);

					}
					/*if(limit>warnBean.getNum()){
						warnBean.setNum(limit);
					}*/
					if(limit<end==false){
						break ;
					}
				} while (!list.isEmpty());
				//贸易-S
				do {

					list = warnEnterpriseRecordMapper.getTradeS(limit, 100, null);
					limit += 100;
					for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
						List<WarnEnterpriseRecord> qryList = new ArrayList<>();
						warnJiSuanBean = new WarnJiSuanBean();
						warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
						warnJiSuanBean.setWarnEnterpriseRecords(qryList);
						warnJiSuanBean.setSubjectCode(TableNameEnum.S_TYPE.getCode());
						warnJiSuanBean.setWarnEnterpriseRecordMapper(warnEnterpriseRecordMapper);
						calculationService.count(warnJiSuanBean);

					}
					/*if(limit>warnBean.getNum()) {
						warnBean.setNum(limit);
					}*/
					if(limit<end==false) {
						break ;
					}
				} while (!list.isEmpty());
				break;
			case services:
				//limit = 0;
				do {

					list = warnEnterpriseRecordMapper.getService(limit, 100, null);
					limit += 100;
					/*if(list.size()==0){
						warnBean.setB(false);
					}*/
					for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
						List<WarnEnterpriseRecord> qryList = new ArrayList<>();
						warnJiSuanBean = new WarnJiSuanBean();
						warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
						warnJiSuanBean.setWarnEnterpriseRecords(qryList);
						warnJiSuanBean.setSubjectCode(TableNameEnum.F_TYPE.getCode());
						warnJiSuanBean.setWarnEnterpriseRecordMapper(warnEnterpriseRecordMapper);
						calculationService.count(warnJiSuanBean);

					}
					/*if(limit>warnBean.getNum()){
						warnBean.setNum(limit);
					}*/
					if(limit<end==false){
						break ;
					}
				} while (list.size() != 0);
				break;
		}


	}

	public int saveNoEditRecords(RuleTypeEnum ruleType, int limit, int end, boolean b) {





		WarnJiSuanBean warnJiSuanBean = new WarnJiSuanBean();
		List<String> companyCodes = new ArrayList<>();
		Integer start =0;// warnEnterpriseRecordMapper.getMaxWarnCode();
		//待插入
		List<WarnEnterpriseRecord> list = new ArrayList<>();
		switch (ruleType) {
			case industrial:
				//int limit = 0;
				do {
					list = warnEnterpriseRecordMapper.getIndustrial(limit, 100,null);
					limit += 100;
					if(limit<=end==false){
						return 0;
					}
					if(list.size()==0){
						b=false;
					}


					for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
						List<WarnEnterpriseRecord> qryList = new ArrayList<>();
						warnJiSuanBean = new WarnJiSuanBean();
						warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
						warnJiSuanBean.setWarnEnterpriseRecords(qryList);
						warnJiSuanBean.setSubjectCode(TableNameEnum.B_TYPE.getCode());
						calculationService.count(warnJiSuanBean);
						for (WarnEnterpriseRecord record : warnJiSuanBean.getWarnEnterpriseRecords()) {
							record.setId(generateSnowFlake());
							record.setWarnCode(generateWarnCode(record.getDimension(), start, String.valueOf(new DateTime(record.getCreateTime()).getYear())));
							record.setProvince(warnEnterpriseRecord.getProvince());
							record.setCity(warnEnterpriseRecord.getCity());
							record.setArea(warnEnterpriseRecord.getArea());
							record.setCompanyName(warnEnterpriseRecord.getCompanyName());
							record.setRegionArea(warnEnterpriseRecord.getRegionArea());
							record.setCompanyCode(warnEnterpriseRecord.getCompanyCode());
							record.setMajorClassify(TableNameEnum.B_TYPE.getType());
							record.setWarnFactor(String.format("%.1f", Double.parseDouble(record.getWarnFactor())));
							if (record.getWarnType() != null) {
								record.setWarningIndex(String.format("%.1f", generateWarnIndex(record.getWarnType(), record.getWarnFactor())));
							} else {
								record.setWarningIndex("0.0");
							}
							start++;
						}
						log.info(">>>>>>>>>{}企业预警{}条", warnJiSuanBean.getSurveyobjectid(), warnJiSuanBean.getWarnEnterpriseRecords().size());
						if (!warnJiSuanBean.getWarnEnterpriseRecords().isEmpty()) {
							System.out.println(warnJiSuanBean.getWarnEnterpriseRecords());
							//warnEnterpriseRecordMapper.insertBatch(warnJiSuanBean.getWarnEnterpriseRecords());
						}
					}
				} while (!list.isEmpty());
				break;
			case investment:
				//limit = 0;
				do {
					list = warnEnterpriseRecordMapper.getInvest(limit, 100, null);
					limit += 100;
					if(limit<=end==false){
						return 0;
					}
					if(list.size()==0){
						b=false;
					}
					for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
						List<WarnEnterpriseRecord> qryList = new ArrayList<>();
						warnJiSuanBean = new WarnJiSuanBean();
						warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
						warnJiSuanBean.setWarnEnterpriseRecords(qryList);
						warnJiSuanBean.setSubjectCode(TableNameEnum._206_TYPE.getCode());
						calculationService.count(warnJiSuanBean);
						for (WarnEnterpriseRecord record : warnJiSuanBean.getWarnEnterpriseRecords()) {
							record.setId(generateSnowFlake());
							record.setWarnCode(generateWarnCode(record.getDimension(), start, String.valueOf(new DateTime(record.getCreateTime()).getYear())));
							record.setProvince(warnEnterpriseRecord.getProvince());
							record.setCity(warnEnterpriseRecord.getCity());
							record.setArea(warnEnterpriseRecord.getArea());
							record.setCompanyName(warnEnterpriseRecord.getCompanyName());
							record.setRegionArea(warnEnterpriseRecord.getRegionArea());
							record.setCompanyCode(warnEnterpriseRecord.getCompanyCode());
							record.setMajorClassify(TableNameEnum._206_TYPE.getType());
							record.setProjectCode(warnEnterpriseRecord.getProjectCode());
							record.setProjectName(warnEnterpriseRecord.getProjectName());
							record.setWarnFactor(String.format("%.1f", Double.parseDouble(record.getWarnFactor())));
							if (record.getWarnType() != null) {
								record.setWarningIndex(String.format("%.1f", generateWarnIndex(record.getWarnType(), record.getWarnFactor())));
							} else {
								record.setWarningIndex("0.0");
							}
							start++;
						}
						log.info(">>>>>>>>>{}企业预警{}条", warnJiSuanBean.getSurveyobjectid(), warnJiSuanBean.getWarnEnterpriseRecords().size());
						if (!warnJiSuanBean.getWarnEnterpriseRecords().isEmpty()) {
							System.out.println(warnJiSuanBean.getWarnEnterpriseRecords());
							//warnEnterpriseRecordMapper.insertBatch(warnJiSuanBean.getWarnEnterpriseRecords());
						}
					}
				} while (!list.isEmpty());
				break;
			case trade:
				//limit = 0;
				//贸易-E
				do {
					list = warnEnterpriseRecordMapper.getTradeE(limit, 100, null);
					limit += 100;
					if(limit<=end==false){
						return 0;
					}
					if(list.size()==0){
						b=false;
					}
					for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
						List<WarnEnterpriseRecord> qryList = new ArrayList<>();
						warnJiSuanBean = new WarnJiSuanBean();
						warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
						warnJiSuanBean.setWarnEnterpriseRecords(qryList);
						warnJiSuanBean.setSubjectCode(TableNameEnum.E_TYPE.getCode());
						calculationService.count(warnJiSuanBean);
						for (WarnEnterpriseRecord record : warnJiSuanBean.getWarnEnterpriseRecords()) {
							record.setId(generateSnowFlake());
							record.setWarnCode(generateWarnCode(record.getDimension(), start, String.valueOf(new DateTime(record.getCreateTime()).getYear())));
							record.setProvince(warnEnterpriseRecord.getProvince());
							record.setCity(warnEnterpriseRecord.getCity());
							record.setArea(warnEnterpriseRecord.getArea());
							record.setCompanyName(warnEnterpriseRecord.getCompanyName());
							record.setRegionArea(warnEnterpriseRecord.getRegionArea());
							record.setCompanyCode(warnEnterpriseRecord.getCompanyCode());
							record.setMajorClassify(TableNameEnum.S_TYPE.getType());
							record.setWarnFactor(String.format("%.1f", Double.parseDouble(record.getWarnFactor())));
							if (record.getWarnType() != null) {
								record.setWarningIndex(String.format("%.1f", generateWarnIndex(record.getWarnType(), record.getWarnFactor())));
							} else {
								record.setWarningIndex("0.0");
							}
							start++;
						}
						log.info(">>>>>>>>>{}企业预警{}条", warnJiSuanBean.getSurveyobjectid(), warnJiSuanBean.getWarnEnterpriseRecords().size());
						if (!warnJiSuanBean.getWarnEnterpriseRecords().isEmpty()) {
							System.out.println(warnJiSuanBean.getWarnEnterpriseRecords());
							//warnEnterpriseRecordMapper.insertBatch(warnJiSuanBean.getWarnEnterpriseRecords());
						}
					}
				} while (!list.isEmpty());
				//贸易-S
				do {
					list = warnEnterpriseRecordMapper.getTradeS(limit, 100, null);
					limit += 100;
					if(limit<=end==false){
						return 0;
					}
					if(list.size()==0){
						b=false;
					}
					for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
						List<WarnEnterpriseRecord> qryList = new ArrayList<>();
						warnJiSuanBean = new WarnJiSuanBean();
						warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
						warnJiSuanBean.setWarnEnterpriseRecords(qryList);
						warnJiSuanBean.setSubjectCode(TableNameEnum.S_TYPE.getCode());
						calculationService.count(warnJiSuanBean);
						for (WarnEnterpriseRecord record : warnJiSuanBean.getWarnEnterpriseRecords()) {
							record.setId(generateSnowFlake());
							record.setWarnCode(generateWarnCode(record.getDimension(), start, String.valueOf(new DateTime(record.getCreateTime()).getYear())));
							record.setProvince(warnEnterpriseRecord.getProvince());
							record.setCity(warnEnterpriseRecord.getCity());
							record.setArea(warnEnterpriseRecord.getArea());
							record.setCompanyName(warnEnterpriseRecord.getCompanyName());
							record.setRegionArea(warnEnterpriseRecord.getRegionArea());
							record.setCompanyCode(warnEnterpriseRecord.getCompanyCode());
							record.setMajorClassify(TableNameEnum.S_TYPE.getType());
							record.setWarnFactor(String.format("%.1f", Double.parseDouble(record.getWarnFactor())));
							if (record.getWarnType() != null) {
								record.setWarningIndex(String.format("%.1f", generateWarnIndex(record.getWarnType(), record.getWarnFactor())));
							} else {
								record.setWarningIndex("0.0");
							}

							start++;
						}
						log.info(">>>>>>>>>{}企业预警{}条", warnJiSuanBean.getSurveyobjectid(), warnJiSuanBean.getWarnEnterpriseRecords().size());
						if (!warnJiSuanBean.getWarnEnterpriseRecords().isEmpty()) {
							System.out.println(warnJiSuanBean.getWarnEnterpriseRecords());
							//warnEnterpriseRecordMapper.insertBatch(warnJiSuanBean.getWarnEnterpriseRecords());
						}
					}
				} while (!list.isEmpty());
				break;
			case services:
				//limit = 0;
				do {
					list = warnEnterpriseRecordMapper.getService(limit, 100, null);
					limit += 100;
					if(limit<=end==false){
						return 0;
					}
					if(list.size()==0){
						b=false;
					}
					for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
						List<WarnEnterpriseRecord> qryList = new ArrayList<>();
						warnJiSuanBean = new WarnJiSuanBean();
						warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
						warnJiSuanBean.setWarnEnterpriseRecords(qryList);
						warnJiSuanBean.setSubjectCode(TableNameEnum.F_TYPE.getCode());
						warnJiSuanBean.setStart(start);
						warnJiSuanBean.setWarnEnterpriseRecordMapper(warnEnterpriseRecordMapper);
						calculationService.count(warnJiSuanBean);
					}
						/*for (WarnEnterpriseRecord record : warnJiSuanBean.getWarnEnterpriseRecords()) {
							record.setId(generateSnowFlake());
							record.setWarnCode(generateWarnCode(record.getDimension(), start, String.valueOf(new DateTime(record.getCreateTime()).getYear())));
							record.setProvince(warnEnterpriseRecord.getProvince());
							record.setCity(warnEnterpriseRecord.getCity());
							record.setArea(warnEnterpriseRecord.getArea());
							record.setCompanyName(warnEnterpriseRecord.getCompanyName());
							record.setRegionArea(warnEnterpriseRecord.getRegionArea());
							record.setCompanyCode(warnEnterpriseRecord.getCompanyCode());
							record.setMajorClassify(TableNameEnum.F_TYPE.getType());
							record.setWarnFactor(String.format("%.1f", Double.parseDouble(record.getWarnFactor())));
							if (record.getWarnType() != null) {
								record.setWarningIndex(String.format("%.1f", generateWarnIndex(record.getWarnType(), record.getWarnFactor())));
							} else {
								record.setWarningIndex("0.0");
							}
							start++;
						}
					}
					log.info(">>>>>>>>>{}企业预警{}条", warnJiSuanBean.getSurveyobjectid(), warnJiSuanBean.getWarnEnterpriseRecords().size());
					if (warnJiSuanBean.getWarnEnterpriseRecords().size() > 0) {
						System.out.println(warnJiSuanBean.getWarnEnterpriseRecords());
						//warnEnterpriseRecordMapper.insertBatch(warnJiSuanBean.getWarnEnterpriseRecords());
					}*/
				} while (list.size() != 0);
				break;
		}
		return 0;
	}





	private String generateWarnCode(String dimension, Integer start, String year) {
		NumberFormat format = NumberFormat.getInstance();
		format.setMinimumIntegerDigits(8);
		format.setGroupingUsed(false);
		return dimension + year + format.format(start);
	}

	private String generateSnowFlake() {
		Snowflake snowflake = IdUtil.getSnowflake(1, 1);
		return snowflake.nextIdStr();
	}

	private Double generateWarnIndex(String key, String factor) {
		Double weight = EnumUtils.getEnum(RuleEnum.class, key.toUpperCase()).getWeight();
		return Double.parseDouble(factor) * weight;
	}

	private int find(String companyCode, List<WarnEnterpriseRecord> lst) {
		int res = -1;
		for (int i = 0; i < lst.size(); i++) {
			if (lst.get(i).getCompanyCode().equals(companyCode)) {
				res = i;
				break;
			}
		}
		return res;
	}


	public static void main(String[] args) {
		int stt=10000;

		int n=421200;

		for(int i=0;i<(n);i += stt){
			try {

				//i += stt;

				System.out.println(i+ "   "+stt);
			}catch (Exception E){}
		}



	}


}